package com.xhwl.centiotdata.handler.eesd.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.eesd.EesdFactory;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.cent.IBiEnvCentService;
import com.xhwl.centiotdata.suppliersystem.eesd.growatt.GrowattApiHandler;
import com.xhwl.common.dto.cent.iotdata.growatt.GrowattReqConfigDTO;
import com.xhwl.common.dto.cent.iotdata.growatt.OdsPhotovoltaicsMetricsDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/06/ 11:31
 */
@Slf4j
@Service
public class EesdGrowattHandler extends EesdHandler {

    @Resource
    private GrowattApiHandler growattApiHandler;

    @Resource
    private IBiEnvCentService biEnvCentService;

    /**
     * id:产品id，来源cent_device库中device_product表中的数据
     */
    @Override
    public void afterPropertiesSet() {
        EesdFactory.register(EesdMethodEnum.GROWATT.getId(), this);
    }

    /**
     * 同步光伏
     * @param config
     */
    @Override
    public void pullPhotovoltaics(DeviceProjSupplierCategory config) {
        List<OdsPhotovoltaicsMetricsDTO> list = getPhotovoltaicsList(config);
        if (!CollectionUtils.isEmpty(list)) {
            String dataStr = JSONObject.toJSONString(list);
            log.info("光伏数据 开始： {}", dataStr);
            biEnvCentService.asyncPhotovoltaics(dataStr);
        }
    }

    @Override
    public void pullPhotovoltaicsHour(DeviceProjSupplierCategory config) {
        List<OdsPhotovoltaicsMetricsDTO> list = getPhotovoltaicsList(config);
        if (!CollectionUtils.isEmpty(list)) {
            String dataStr = JSONObject.toJSONString(list);
            log.info("光伏趋势数据 开始： {}", dataStr);
            biEnvCentService.asyncPhotovoltaicsHour(dataStr);
        }
    }



    private List<OdsPhotovoltaicsMetricsDTO> getPhotovoltaicsList(DeviceProjSupplierCategory config) {
        List<OdsPhotovoltaicsMetricsDTO> list = new ArrayList<>();
        GrowattReqConfigDTO dto =
                JSONObject.parseObject(JSON.toJSONString(config.getProperties()), GrowattReqConfigDTO.class);
        dto.setProjectId(config.getProjectId());
        dto.setProductId(config.getProductId());
        //同步max设备以及状态
        try {
            ResultJson result = growattApiHandler.syncMaxDevice(dto);
            if (result.getState()) {
                log.info("同步max设备结果成功:{}", result);
            }
        } catch (Exception e) {
            log.error("同步max设备结果 失败： {}",  e.getMessage());
        }
        try {
            OdsPhotovoltaicsMetricsDTO o1 = growattApiHandler.getPlantInfo(dto, 0);
            if (!ObjectUtils.isEmpty(o1)) {
                log.info("获取电站数据:{}", JSON.toJSONString(o1));
                list.add(o1);
            }
        } catch (Exception e) {
            log.error("获取电站数据 失败： {}",  e.getMessage());
        }
        try {
            OdsPhotovoltaicsMetricsDTO o2 = growattApiHandler.getInverterInfo(dto, 1);
            if (!ObjectUtils.isEmpty(o2)) {
                log.info("获取逆变器MAX数据:{}", JSON.toJSONString(o2));
                list.add(o2);
            }
        } catch (Exception e) {
            log.error("获取逆变器MAX数据 失败： {}",  e.getMessage());
        }
        /*try {
            OdsPhotovoltaicsMetricsDTO o3 = growattApiHandler.getEnvInfo(dto, 3);
            if (!ObjectUtils.isEmpty(o3)) {
                log.info("获取环境检测仪数据:{}", JSON.toJSONString(o3));
                list.add(o3);
            }
        } catch (Exception e) {
            log.error("获取环境检测仪数据 失败： {}",  e.getMessage());
        }*/
        return list;
    }
}
